<?php

/**
 * 自动生成 WxEventTreasureUserAssistService
 * 
 * Apps/Common/Service/WxEventTreasureUserAssistService.class.php
 * Zix @ 2015-08-26
 */

namespace Common\Service;

class WxEventTreasureUserAssistService {

  private static $instance;

  public static function instance() {
    if (self::$instance == null) {
      $instance = new WxEventTreasureUserAssistService();
    }

    return $instance;
  }

  function get_default_row() {
    return array(
      'id' => '',
      'user_id' => '',
      'nickname' => '',
      'icon' => '',
      'assist_user_id' => '',
      'point' => '',
      'create_time' => '',
    );
  }

  function get_by_id($id) {
    $WxEventTreasureUserAssist = M('WxEventTreasureUserAssist');
    $data = $WxEventTreasureUserAssist->find($id);
    return $data ? $data : array();
  }

  function get_by_cond($config) {
    $default = array(
      'status' => '',
      'event_id' => '',
      'assist_user_id' => '',
      'page' => 1,
      'page_size' => 6,
      'count' => FALSE,
      'order' => 'DESC',
      'sort' => 'id',
    );

    $config = extend($config, $default);

    $WxEventTreasureUserAssist = M('WxEventTreasureUserAssist');

    $where = array();

    if (!empty($config['status'])) {
      $where['etua.status'] = $config['status'];
    }
    
    if (!empty($config['event_id'])) {
      $where['etua.event_id'] = $config['event_id'];
    }
    
    if (!empty($config['assist_user_id'])) {
      $where['etua.assist_user_id'] = $config['assist_user_id'];
    }

    if ($config['count']) {
      return $WxEventTreasureUserAssist->alias('etua')->where($where)->count();
    } else {
      $order = 'etua.' .$config['sort'] . ' ' . $config['order'];
      $limit = ($config['page'] - 1 ) * $config['page_size'] . ' , ' . $config['page_size'];
      $data = $WxEventTreasureUserAssist
        ->alias('etua')
        ->field('etua.* , u.nickname as nickname , u.icon as icon')
        ->join('mer_user u on u.id = etua.user_id')
        ->where($where)
        ->limit($limit)->order($order)->select();
      //echo $WxEventTreasureUserAssist->_sql();
      return $data ? $data : array();
    }
  }

  function create($data, $is_ajax = true) {
    $WxEventTreasureUserAssist = M('WxEventTreasureUserAssist');
    $ret = $WxEventTreasureUserAssist->add($data);
    if ($is_ajax) {
      if ($ret) {
        return ajax_arr('添加成功', TRUE, array(
          'id' => $ret
        ));
      } else {
        return ajax_arr('添加失败', FALSE);
      }
    } else {
      if ($ret) {
        return $ret;
      } else {
        return 0;
      }
    }
  }

  function update($id, $data) {
    $WxEventTreasureUserAssist = M('WxEventTreasureUserAssist');

    $ret = $WxEventTreasureUserAssist->where("id = %d", $id)->save($data);
    if ($ret) {
      return ajax_arr('编辑成功', TRUE);
    } else {
      return ajax_arr('编辑失败', FALSE);
    }
  }

  function delete($ids) {
    $WxEventTreasureUserAssist = M('WxEventTreasureUserAssist');
    $ret = $WxEventTreasureUserAssist->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }
  
}
